package XPStruct;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

import org.msgpack.core.MessagePack;
import org.msgpack.core.MessageUnpacker;

interface XUnpackerCallback {
    void onStart(MessageUnpacker unpacker) throws IOException;
}

//傀儡设备(手机端) 发送到 电脑设备(机位) 的返回值
public class C2SStruct {
    public String      action;

    public String      guid;  //请求(响应) 编号

    public String      kid;	//设备(进程) 编号

    public long        time;  //发送(响应) 时间

    public byte[]      retv;

    public String      error;

    public C2SStruct(){

    }
    public C2SStruct( ByteBuffer message ){
        try {
            MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(
                                                             message);
            if (6 != unpacker.unpackArrayHeader()) // 开始一个包含5个元素的数组
                return;
            action  = unpacker.unpackString();
            guid    = unpacker.unpackString();  //请求(响应) 编号
            kid     = unpacker.unpackString();	//设备(进程) 编号
            time    = unpacker.unpackLong();    //发送(响应) 时间
            //
                int binSize = 0;
                if (0 != (binSize = unpacker.unpackBinaryHeader())) //长度
                    retv    = unpacker.readPayload(binSize);
                else
                    retv    = null;
            //
            error   = unpacker.unpackString();
            //
            unpacker.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //------------------------------------------------------------------------------- 公用方法

    private static byte[] MSGBinary(MessageUnpacker unpacker){
        try {
            int binSize = 0;
            if (0 == (binSize = unpacker.unpackBinaryHeader())) //长度
                return null;

            //转换返回值
            byte [] retval = unpacker.readPayload(binSize);

            //返回
            return retval;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static byte[] MSGBinary( byte[] inByteS ){
        try {
            if (null == inByteS || 0 == inByteS.length)
                return null;

            MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(
                                                             inByteS);
            //转换返回值
            byte [] retval = MSGBinary(unpacker);

            //关闭
            unpacker.close();
            //返回
            return retval;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static void MSGStart2CB( byte[] inByteS , XUnpackerCallback xCallback){
        try {
            if (null == inByteS || 0 == inByteS.length)
                return ;
            MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(
                                                             inByteS);
            //转换返回值
            xCallback.onStart(unpacker);
            //关闭
            unpacker.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //------------------------------------------------------------------------------- 以下是将返回值, 转换成各种类型

    //转换Long型
    public long ConvertByLong(){
        try {
            //返回
            AtomicLong retval = new AtomicLong();
            //开始分析
            MSGStart2CB(retv, unpacker -> {
                if (null == unpacker)
                    return;
                retval.set(unpacker.unpackLong());
            });
            //返回
            return retval.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //转换逻辑型
    public boolean ConvertByBoolen(){
        try {
            //返回
            AtomicBoolean retval = new AtomicBoolean(false);
            //开始分析
            MSGStart2CB(retv, unpacker -> {
                if (null == unpacker)
                    return;
                retval.set(unpacker.unpackBoolean());
            });
            //返回
            return retval.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    //转换文本型
    public String ConvertByString(){
        try {
            //解包, 字节组
            byte [] retval = MSGBinary(retv);
            //检测是否解包
            if (null == retval || 0 == retval.length)
                return "";
            //返回
            return new String(retval);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    //转换文本型数组
    public ArrayList<String> ConvertByStringArray(){
        try {
            if (null == retv || 0 == retv.length)
                return new ArrayList<>();
            MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(
                                                                retv);
            int arraySize = 0;
            if (0 == (arraySize = unpacker.unpackArrayHeader())) //长度
                return new ArrayList<>();
            //返回值
            ArrayList<String> retvalS = new ArrayList<>();
            //读取每个成员
            for (int aIndex = 0; aIndex < arraySize; aIndex++) {
                //解包, 字节组
                byte [] retval = MSGBinary(unpacker);
                //检测是否解包
                if (null == retval || 0 == retval.length)
                    break;
                //返回
                retvalS.add(new String(retval));
            }

            //关闭
            unpacker.close();

            //返回
            return retvalS;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }






}
